//通用的消息展示代码
var show_status = document.getElementById("show_status");
function show(text){
    show_status.innerText = text;
}

var input_name = document.getElementById("input_name");
var input_url = document.getElementById("input_url");
var choose_category = document.getElementById("category");

//打开书签主页按钮
document.getElementById("manager").onclick = function (){
    window.open('/webpage/index/index.html')
}

//关闭按钮
document.getElementById("close").onclick = function () {
    window.close();
};

//退出登录
document.getElementById("logout").onclick = function () {
    api_logout(function (data){
        if(data["code"] === 1){
            alert("退出登录成功")
            window.close()
        }else{
            show(data["msg"])
        }
    })
};

//确认添加按钮
document.getElementById("confirm").onclick = function (){
    var name = input_name.value;
    var url = input_url.value;
    var category = choose_category.value;
    if(is_empty(name)){
        show("请输入标题")
        return
    }
    if(is_empty(url)){
        show("请输入链接")
        return
    }
    api_add_tag(name, url,category, function (data){
        if(data["code"] === 1){
            alert("添加成功")
            window.close()
        }else{
            show(data["msg"])
        }
    })
}

// 请求用户的分类，刷新分类页面
api_get_all_category(function (data){
    if(data['code'] === 1){
        //移除等待的圈圈
        document.getElementById("wait_img").style.display = 'none'
        choose_category.options.remove(0)

        var categories = data['categories'];
        for(var i=0;i<categories.length; i++){
            var category = categories[i]
            choose_category.options.add(new Option(category['category'], category['category_id']));
        }
    }
})

//获取当前页面的标题和链接，展示出来
get_current_page(function (title, url){
    input_name.value = title;
    input_url.value = url;
})